//
//  UIViewController+KLAutoTracker.m
//  RKLFoundationFrame
//
//  Created by RKL on 2018/3/1.
//  Copyright © 2018年 AppFrameWorkDemo. All rights reserved.
//

#import "UIViewController+KLAutoTracker.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "KLAutoTrackerOperation.h"
#import "NSObject+KLAutoTracker.h"

@implementation UIViewController (KLAutoTracker)

+ (void)startTracker
{
    Method viewWillAppearSelector = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method hookViewWillAppearSelector = class_getInstanceMethod(self, @selector(hook_viewWillAppear:));
    method_exchangeImplementations(viewWillAppearSelector, hookViewWillAppearSelector);
    
    Method viewDidLoadSelector = class_getInstanceMethod(self, @selector(viewDidLoad));
    Method hookViewDidLoadSelector = class_getInstanceMethod(self, @selector(hook_viewDidLoad));
    method_exchangeImplementations(viewDidLoadSelector, hookViewDidLoadSelector);
    
    Method viewDidDisappearSelector = class_getInstanceMethod(self, @selector(viewDidDisappear:));
    Method hookViewDidDisappearSelector = class_getInstanceMethod(self, @selector(hook_viewDidDisappear:));
    method_exchangeImplementations(viewDidDisappearSelector, hookViewDidDisappearSelector);
}

- (void)hook_viewWillAppear:(BOOL)animated
{
    if (![[self class] isEqual:[UINavigationController class]]&&
        ![[self class] isEqual:[UITabBarController class]])
    {
        //在这里填写需要插入的代码
        [self sendTrackerData];
    }
    else
    {
        
    }
    
    //执行原来的代码，不影响代码逻辑
    [self hook_viewWillAppear:animated];
}

- (void) hook_viewDidLoad
{
    
    [self hook_viewDidLoad];
}

- (void) hook_viewDidDisappear:(BOOL)animated
{
    
    //执行原来的代码，不影响代码逻辑
    [self hook_viewDidDisappear:animated];
}

#pragma mark - 发送统计数据
- (void)sendTrackerData
{
    NSLog(@"------------%@-----------",self);
    [[KLAutoTrackerOperation sharedInstance] sendTrackerData:nil
                                                        info:nil];
}

@end















